package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.PostLike;
import com.ruoyi.system.domain.PostReward;
import com.ruoyi.system.dto.PostLikeDTO;
import com.ruoyi.system.dto.PostRewardDTO;
import com.ruoyi.system.service.PostsInteractionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 动态互动控制器
 *
 * 说明：当前实现为接口打通示例，返回结构化数据供前端联调展示；
 * 后续可替换为持久化实现（建表、Mapper、Service）
 */
@RestController
@RequestMapping("/system/posts/interaction")
@Api("动态互动")
public class PostsInteractionController extends BaseController {
    /** 互动业务服务 */
    @Autowired
    private PostsInteractionService interactionService;
    /** 点赞列表（分页） */
    @ApiOperation("点赞列表")
    @PreAuthorize("@ss.hasPermi('system:posts:list')")
    @GetMapping("/likes")
    public TableDataInfo likes(Long postId, Integer pageNum, Integer pageSize) {
        startPage();
        PostLike query = new PostLike();
        query.setPostId(postId);
        List<PostLikeDTO> list = interactionService.selectLikes(query);
        return getDataTable(list);
    }

    /** 打赏列表 */
    @ApiOperation("打赏列表")
    @PreAuthorize("@ss.hasPermi('system:posts:list')")
    @GetMapping("/rewards")
    public AjaxResult rewards(Long postId) {
        startPage();
        PostReward query = new PostReward();
        query.setPostId(postId);
        List<PostRewardDTO> list = interactionService.selectRewards(query);
        BigDecimal totalAmount = interactionService.sumRewardAmount(postId);
        java.util.HashMap<String, Object> data = new java.util.HashMap<>();
        data.put("rows", list);
        data.put("total", list.size());
        data.put("totalAmount", totalAmount);
        return AjaxResult.success(data);
    }

    /** 获取互动统计（点赞/评论/打赏/分享） */
    @ApiOperation("互动统计")
    @PreAuthorize("@ss.hasPermi('system:posts:list')")
    @GetMapping("/stats/{postId}")
    public AjaxResult stats(@PathVariable("postId") Long postId) {
        return AjaxResult.success(new java.util.HashMap<String, Object>() {{
            put("likeCount", 0);
            put("commentCount", 0);
            put("rewardCount", 0);
            put("shareCount", 0);
        }});
    }
}
